import Path from 'node:path';
import fs from "node:fs";

/**
 * 文件对象
 */
class KoaBootFile {

    /**
     * 项目根路径
     * @private
     */
    private static readonly rootPath: string = Path.resolve();

    /**
     * 文件路径
     * @private
     */
    private readonly pathName: string;

    constructor();
    constructor(pathName: string | KoaBootFile);
    constructor(parent: string | KoaBootFile, children: string | KoaBootFile);
    constructor(parent?: string | KoaBootFile, children?: string | KoaBootFile) {
        if (parent instanceof KoaBootFile && typeof children == 'string') {
            this.pathName = Path.join(parent.getPath(), children);
        } else if (parent instanceof KoaBootFile && children instanceof KoaBootFile) {
            this.pathName = Path.join(parent.getPath(), children.getPath());
        } else if (typeof parent == 'string' && typeof children == 'string') {
            this.pathName = Path.join(parent, children);
        } else {
            this.pathName = parent as string;
        }
    }

    /**
     * 读物文件内容为字符串
     * @param pathName
     */
    public readString(pathName?: string | KoaBootFile): string | undefined {
        if (pathName && typeof pathName == 'string') {
            return fs.readFileSync(pathName, 'utf-8');
        }
        if (pathName instanceof KoaBootFile) {
            return fs.readFileSync(pathName.getPath(), 'utf-8');
        }
        return fs.readFileSync(this.getPath(), 'utf-8')
    }

    public getPath(): string {
        return this.pathName;
    }

    public static getRootPath(): string {
        return this.rootPath;
    }

    public static getResourcePath(): string {
        return Path.join(this.rootPath, "src", "main", "resource");
    }

}

export default KoaBootFile;